package com.qianlima.upload.ui.aspect;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.time.Duration;

/**
 * aop 切面，统计方法耗时
 * @author allms
 */
@Component
@Aspect
@Slf4j
public class DurationAspect {


    @Around("execution(public * com.qianlima.upload.ui.controller.UploadController.uploadFile*(..))")
    public Object upload(ProceedingJoinPoint joinPoint) {
        long startTime = System.nanoTime();
        log.info("多文件上传：" + joinPoint.getSignature().getName());
        try {
            return joinPoint.proceed();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        } finally {
            Duration time = Duration.ofNanos(System.nanoTime() - startTime);
            log.info("多文件上传结束，消耗了：" + time.getSeconds() + "s");
        }

    }


    @Around("execution(public * com.qianlima.upload.ui.controller.UploadController.check*(..))")
    public Object check(ProceedingJoinPoint joinPoint) {
        long startTime = System.nanoTime();
        log.info("key检查：" + joinPoint.getSignature().getName());
        try {
            return joinPoint.proceed();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        } finally {
            Duration time = Duration.ofNanos(System.nanoTime() - startTime);
            log.info("key检查结束，消耗了：" + time.getSeconds() + "s");
        }

    }

    @Around("execution(public * com.qianlima.upload.ui.controller.UploadController.fragUploadFile*(..))")
    public Object fragUpload(ProceedingJoinPoint joinPoint) {
        long startTime = System.nanoTime();
        log.info("分片上传：" + joinPoint.getSignature().getName());
        try {
            return joinPoint.proceed();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        } finally {
            Duration time = Duration.ofNanos(System.nanoTime() - startTime);
            log.info("分片上传，消耗了：" + time.getSeconds() + "s");
        }

    }
}
